package com.golddaymicromall.activity.jifenconfirmpay;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.golddaymicromall.activity.R;
import com.golddaymicromall.application.MyApplication;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.bean.TePinInfo;
import com.golddaymicromall.util.ActivityController;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;

public class BonusPointsConfirmPayPwdActivity extends BaseActivity implements View.OnClickListener {
    private TextView mCustomerid;
    private EditText mPayPassword;
    private Button mConfirmPay;
    private LinearLayout mPayPasswordBack;
    String id;
    private String customerid;
    private String orderId;
    String logid;
    String msaleflag;

    List<TePinInfo> list;
    String text;
    String flag = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bonuspoints_confirm_pay_pwd);
        initView();
    }

    private void initView() {
        mCustomerid = (TextView) findViewById(R.id.pay_customerid);
        mPayPassword = (EditText) findViewById(R.id.pay_password);
        mConfirmPay = (Button) findViewById(R.id.querenzhifu);
        mPayPasswordBack = (LinearLayout) findViewById(R.id.paypasswordback);
        mConfirmPay.setOnClickListener(this);
        mPayPasswordBack.setOnClickListener(this);
        id = customerid = SPUtils.get(getApplicationContext(), "Id", "").toString();
        customerid = SPUtils.get(getApplicationContext(), "Customerno", "").toString();
        msaleflag = SPUtils.get(getApplicationContext(), "saleflag", "").toString();
        mCustomerid.setText(customerid);
        orderId = getIntent().getStringExtra("orderId");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.paypasswordback:
                finish();
                break;
            case R.id.querenzhifu:
                attemptValidatePayment();
                MyApplication.getInstance().attemptRiZhi(SPUtils.get(getApplicationContext(), "Customerno", "").toString(),
                        SPUtils.get(getApplicationContext(), "Customerno", "").toString() + "抢购，验证支付，订单编号：" + orderId);
                break;
        }
    }


    public void attemptPayment(String orderid) {
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.order.payment");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("id", orderid);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);
        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .doOnSubscribe(() -> showLoading())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    dismissLoading();
                    if (!"".equals(result)) {

                        //0订单已删除
                        //1支付成功
                        //-6库存不足
                        //-1地址为空
                        //-5积分不足
                        if ("1".equals(result)) {
                            Toast.makeText(BonusPointsConfirmPayPwdActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                            finish();
                            ActivityController.getInstance().popActivity();
                            ActivityController.getInstance().popActivity();
                        } else if ("0".equals(result)) {
                            Toast.makeText(BonusPointsConfirmPayPwdActivity.this, "余额不足,请充值", Toast.LENGTH_SHORT).show();
                        } else if ("-5".equals(result)) {
                            Toast.makeText(BonusPointsConfirmPayPwdActivity.this, "积分不足", Toast.LENGTH_SHORT).show();
                        } else if ("-6".equals(result)) {
                            Toast.makeText(BonusPointsConfirmPayPwdActivity.this, "库存不足", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(BonusPointsConfirmPayPwdActivity.this, "支付失败", Toast.LENGTH_SHORT).show();

                        }
                    }
                }, throwable -> {
                    dismissLoading();
                    showError();
                    throwable.printStackTrace();
                });

    }

    public void attemptValidatePayment() {
        logid = SPUtils.get(getApplicationContext(), "Id", "").toString();
        String paypwd = mPayPassword.getText().toString();
        if ("".equals(paypwd)) {
            Toast.makeText(this, "支付密码不能为空!", Toast.LENGTH_SHORT).show();
            return;
        }
        ValidatePassword(logid, paypwd);

    }

    public void ValidatePassword(String logid, String paymentpwd) {

        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.password.get");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("id", logid);
        form.put("checkpoint", "jifen");
        form.put("trandword", paymentpwd);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);
        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .doOnSubscribe(() -> showLoading())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    dismissLoading();
                    if (!"".equals(result)) {
                        result = result.replace(".0", "");
                        if (Integer.parseInt(result) == 0) {
                            Toast.makeText(BonusPointsConfirmPayPwdActivity.this, "支付密码错误!", Toast.LENGTH_SHORT).show();
                            return;
                        } else if (Integer.parseInt(result) == 1) {
                            attemptPayment(orderId);
                            MyApplication.getInstance().attemptRiZhi(SPUtils.get(getApplicationContext(), "Customerno", "").toString(),
                                    SPUtils.get(getApplicationContext(), "Customerno", "").toString() + "积分，支付成功，订单编号：" + orderId);
                        } else if (Integer.parseInt(result) == 2) {
                            Toast.makeText(BonusPointsConfirmPayPwdActivity.this, "账户异常,请重新输入!", Toast.LENGTH_SHORT).show();
                            return;
                        } else if (Integer.parseInt(result) == 3) {
                            Toast.makeText(BonusPointsConfirmPayPwdActivity.this, "账户余额不足!", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                }, throwable -> {
                    dismissLoading();
                    showError();
                    throwable.printStackTrace();
                });

    }

}
